/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.webapp.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import vn.webapp.dto.DataPage;
import vn.webapp.model.BusyTeachers;
import vn.webapp.model.Teachers;
import vn.webapp.model.TeachersToBusy;
import vn.webapp.model.Timetabling;
import vn.webapp.model.Timetablingraw;

@Repository("teachersDAO")
//@SuppressWarnings({"unchecked", "rawtypes"})
@Transactional
public class TeachersDAOImpl extends BaseDao implements TeachersDAO {

    @Autowired
    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
    
    @Override
    public List<Teachers> list() {
        try {
            begin();
            Criteria criteria = getSession().createCriteria(Teachers.class);
            //criteria.setMaxResults(10);
            //criteria.setFirstResult(0);
            List<Teachers> teachers = criteria.list();
            commit();
            return teachers;
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return null;
        } finally {
            flush();
            close();
        }
    }
    
    @Override
    public Teachers viewDetail(String id) {
        Query q = sessionFactory.getCurrentSession().createQuery(
                "FROM Teachers c WHERE c.teacher_id = '" + id + "'");
        return (Teachers) q.uniqueResult();
    }
    
    @Override
    public Teachers checkExistsEmail(String email, int id) {
        Query q = sessionFactory.getCurrentSession().createQuery(
                "FROM Teachers c WHERE c.email = '" + email + "' AND c.teacher_id != '" + id + "'");
        return (Teachers) q.uniqueResult();
    }
    
    @Override
    public int updateTheTeacher(String name, String email, int id){
        Query query = sessionFactory.getCurrentSession().createQuery(
                "UPDATE Teachers SET name ='" + name + "', email = '" + email + "' WHERE teacher_id = '" + id + "'");
        int result = query.executeUpdate();
        return result;
    }
    
    @Override
    public int removeTeacher(int id) {
        Query query = sessionFactory.getCurrentSession().createQuery(
                "DELETE from Teachers r WHERE r.teacher_id ='" + id + "'");
        int result = query.executeUpdate();
        return result;
    }
    
    @Override
    public int saveATeacher(Teachers teachers)
    {
        try {
           begin();
           int id = 0; 
           id = (int)getSession().save(teachers);
           commit();
           return id;           
        } catch (Exception e) {
            System.err.println("Got an exception!");
            System.err.println(e.getMessage());
            return 0;
        }
    }
    
    @Override
    public int count(){
        //int size = 0;
         try {
            begin();
            Criteria criteria = getSession().createCriteria(Teachers.class);            
            List<Teachers> teachers = criteria.list();
            commit();
            return teachers.size();
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return 0;
        } finally {
            flush();
            close();
        }
    }
    
    @Override
    @Transactional
    public List<Teachers> filter(int page, int pageSize) {
        try {
            begin();
            Criteria criteria = getSession().createCriteria(Teachers.class);
            criteria.setMaxResults(pageSize);
            if (page > 0) {
                criteria.setFirstResult(page * pageSize);
            } else {
                criteria.setFirstResult(page);
            }
            List<Teachers> teachers = criteria.list();
            commit();
            return teachers;
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return null;
        } finally {
            flush();
            close();
        }
    }
    
    @Override
    public List<BusyTeachers> getABusyTeacherByTeacherId(int iTeacherId, int iSessionId, int Day) {
//        Query q = sessionFactory.getCurrentSession().createQuery(
//                "FROM Busyteacher c WHERE c.teacher_id = '" + iTeacherId + "' AND c.session_id != '" + iSessionId + "' AND c.day != '" + Day + "'");
//        return (BusyTeachers) q.uniqueResult();
        
        try {
            begin();
            Criteria criteria = getSession().createCriteria(BusyTeachers.class, "busyteachers");
            criteria.add(Restrictions.eq("busyteachers.teacher_id",iTeacherId));
            criteria.add(Restrictions.eq("busyteachers.session_id",iSessionId));
            criteria.add(Restrictions.eq("busyteachers.day",Day));

            List<BusyTeachers> busyteachers = criteria.list();
            commit();
            return busyteachers;
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return null;
        } finally {
            flush();
            close();
        }
    }
    
    @Override
    public List<BusyTeachers> listAllBusyTeachers()
    {
        try {
            begin();
            Criteria criteria = getSession().createCriteria(BusyTeachers.class);
            List<BusyTeachers> teachers = criteria.list();
            commit();
            return teachers;
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return null;
        } finally {
            flush();
            close();
        }
    }
    
    public int editBusyTeacherByTeacherId(BusyTeachers oBusyTeacher)
    {
        try {
           begin();
           int id = 0; 
           id = (int)getSession().save(oBusyTeacher);
           commit();
           return id;           
        } catch (Exception e) {
            System.err.println("Got an exception!");
            System.err.println(e.getMessage());
            return 0;
        }
    }
    
    @Override
    public int removeBusyTeacher(int iBusyTeacherId ){
        BusyTeachers c = new BusyTeachers();
        c.setBusyteacher_id(iBusyTeacherId);        
        try {
            begin();
            getSession().delete(c);
            commit();
            return 1;
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return 0;
        } finally {
            flush();
            close();
        }
    }
    
    @Override
    public List<TeachersToBusy> getAllTeachersToBusy()
    {
        try {
            begin();
            Criteria criteria = getSession().createCriteria(TeachersToBusy.class);
            List<TeachersToBusy> teachersToBusy = criteria.list();
            commit();
            return teachersToBusy;
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return null;
        } finally {
            flush();
            close();
        }
    }

}
